﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ReflectionStudio.Core.Helpers;
using System.Xml;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Media;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFSPLAN : DocumentBase
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        int lastindex = -1;
        double OldValue = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAFSPLANData m_Data, m_DataX;
        public DataSet dsParamaters;
        YCAFSPLANReport m_rpt;
        DataTable tbUser = null;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetDetail");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }

        bool IsAutoBackCalulate
        {
            get
            {
                if (ChkAutoBackCalulate.IsChecked == true)
                {
                    return true;
                }
                return false;
            }
            set
            {
                if (value)
                {
                    ChkAutoBackCalulate.IsChecked = true;
                }
                else
                {
                    ChkAutoBackCalulate.IsChecked = false;
                }
            }
        }
        string EKPBEKZ
        {
            get
            {
                if (ChkEKPBEKZ.IsChecked == true)
                {
                    return "N";
                }
                return " ";
            }
        }
        string m_EKPME3;
        string EKPME3
        {
            get
            {
                m_EKPME3 = "N";
                if (ChkEKPME3.IsChecked == true)
                {
                    m_EKPME3 = "Y";
                }
                return m_EKPME3;
            }
            set
            {
                m_EKPME3 = value;
            }
        }
        double wgPackage
        {
            get
            {
                if (txtPackgewg.Text.ToString().Trim() == "")
                {
                    txtPackgewg.Text = "0";
                }
                return System.Convert.ToDouble(txtPackgewg.Text.ToString());
            }
            set
            {
                if (value.ToString().Trim() == "")
                {
                    value = 0;
                }
                txtPackgewg.Text = value.ToString();
            }
        }
        double EKNQTY
        {
            get
            {
                if (txtEKNQTy.Text.ToString().Trim() == "")
                {
                    txtEKNQTy.Text = "0";
                }
                return System.Convert.ToDouble(txtEKNQTy.Text.ToString());
            }
            set
            {
                if (value.ToString().Trim() == "")
                {
                    value = 0;
                }
                txtEKNQTy.Text = value.ToString();
            }
        }
        double NWidth
        {
            get
            {
                if (txtNWidth.Text.ToString().Trim() == "")
                {
                    txtNWidth.Text = "0";
                }
                return System.Convert.ToDouble(txtNWidth.Text.ToString());
            }
            set
            {
                if (value.ToString().Trim() == "")
                {
                    value = 0;
                }
                txtNWidth.Text = value.ToString();
            }
        }
        double Width
        {
            get
            {
                if (txtWidth.Text.ToString().Trim() == "0")
                {
                    txtWidth.Text = txtNWidth.Text;
                }
                return System.Convert.ToDouble(txtWidth.Text.ToString());
            }
            set
            {
                if (value.ToString().Trim() == "")
                {
                    value = NWidth;
                }
                txtWidth.Text = value.ToString();
            }
        }
        double WidthRatio
        {
            get
            {
                if (txtWidthRatio.Text.ToString().Trim() == "")
                {
                    return 1;
                }
                if (txtWidth.Text.ToString().Trim() == "")
                {
                    return 1;
                }
                if (txtNWidth.Text.ToString().Trim() == "")
                {
                    return 1;
                }
                return System.Convert.ToDouble(txtWidthRatio.Text);
            }
            set
            {
                txtWidthRatio.Text = value.ToString();
            }
        }
        string KRS
        {
            get
            {

                return Helper.Right("  " + txtEKKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtEKKRS.Text = value;
            }
        }
        string LFD
        {
            get
            {

                return Helper.Right("000000 " + txtEKLFD.Text.ToString().Trim(), 6);
            }
            set
            {
                txtEKLFD.Text = value;
            }
        }
        double ConvertRatio = 1;
        CollectionViewSource vwABPOS_MAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS_MAT");
            }
        }
        DataRow dwABPOS_MAT
        {
            get
            {
                if (vwABPOS_MAT.View == null)
                    return null;
                if ((DataRowView)vwABPOS_MAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS_MAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }

        CollectionViewSource vwYARNMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwYARNMatrix");
            }
        }
        CollectionViewSource vwFABMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFABMatrix");
            }
        }
        CollectionViewSource vwMATMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMATMatrix");
            }
        }

        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECKMAT");
            }
        }
        DataRow dwFDECKMAT
        {
            get
            {
                if (vwFDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwFDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECKMAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECKMAT");
            }
        }
        DataRow dwWDECKMAT
        {
            get
            {
                if (vwWDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwWDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECKMAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECK");
            }
        }
        DataRow dwWDECK
        {
            get
            {
                if (vwWDECK.View == null)
                    return null;
                if ((DataRowView)vwWDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECK.View.CurrentItem).Row;
            }
        }

        BindingListCollectionView cvWDECK
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(WDECKDataGrid.ItemsSource);
            }
        }
        BindingListCollectionView cvABAUFM
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABAUFM.ItemsSource);
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwABKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABKOPF");
            }
        }
        DataRow dwABKOPF
        {
            get
            {
                if (vwABKOPF.View == null)
                    return null;
                if ((DataRowView)vwABKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABKOPF.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                return m_Data.Left(txtFSART.Text.ToString() + "                      ", 20);
            }
        }
        string mtype
        {
            get
            {
                return cbWTYE.Text;
            }
        }
        string user = "";
        #endregion

        public YCAFSPLAN()
        {
            InitializeComponent();
            this.Name = "投产规划";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            m_Data = new YCAFSPLANData(OracleDB, proxy);
            user = Helper.Left(((string)dicParamater["User"]) + "             ", 8);
            m_Data.UserID = user;
            if (user.Trim() == "YCAV")
            {
                txtFSART.Text = "B140400205          ";
                txtEKLFD.Text = "001471";
            }
            IsLoaded = false;
            txtUserID.Text = (string)dicParamater["User"];
            txtPassword.Password = (string)dicParamater["Pwd"];
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsLoaded)
                {
                    SetEnv.Merge(m_Data.LoadAssData());
                    vwEKPOS.View.CurrentChanged += vwEKPOS_CurrentChanged;
                    vwWDECK.View.CurrentChanged += vwWDECK_CurrentChanged;
                    IsLoaded = true;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = m_Data.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A002010"://确定数量等
                        KRS = dwWFEvent["KRS"].ToString();
                        LFD = dwWFEvent["LFD"].ToString();
                        btnQuery_Click(null, null);
                        break;
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneManager.Visibility = Visibility.Hidden;
            ZoneIT.Visibility = Visibility.Hidden;

            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = m_Data.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            txtLogIn.Background = new SolidColorBrush(Colors.Yellow);
            DataSet setResult = m_Data.LogIn(Mandant, userID, SPassword);
            if (setResult.Tables.Count == 0)
                return;
            if (setResult.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            DataRow dwUser = setResult.Tables[0].Rows[0];
            txtSPRACHKZ.Text = dwUser["HRSPRKZ"].ToString();
            m_Data.UserID = userID;
            m_Data.WorkingDate = DateTime.Now.Year.ToString() + m_Data.Right("00" + DateTime.Now.Month.ToString(), 2)
                + m_Data.Right("00" + DateTime.Now.Day.ToString(), 2);
            m_Data.MANDANT = Mandant;
            m_Data.SPRACHKZ = txtSPRACHKZ.Text.Trim();
            switch (dwUser["HRBGRP"].ToString())
            {
                case "EM":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Hidden;
                    ZoneManager.IsEnabled = true;
                    break;
                case "YA":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Visible;
                    ZoneIT.IsEnabled = true;
                    ZoneManager.IsEnabled = true;
                    break;
            }
            ZoneNormal.IsEnabled = true;
            expLog.IsExpanded = false;
            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        }
        void vwWDECK_CurrentChanged(object sender, EventArgs e)
        {
            if (dwWDECK == null)
                return;
            txtFSART.Text = dwWDECK["WDART"].ToString();
            SetData.Clear();
        }
        void vwEKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwEKPOS == null)
                return;
            btnFilterClear_Click(null, null);
            lastindex = vwEKPOS.View.CurrentPosition;
            LastRow = dwEKPOS;
            OldValue = Convert.ToDouble(dwEKPOS["EKPME1"].ToString());
            btnQtyCost_Click(null, null);
            CheckFunction(cbWTYE.Text);
            if (dwEKPOS["EKPAUFKRS"].ToString() == "RP")
            {
                btnUpdateIntex.IsEnabled = false;
                btnApprove.IsEnabled = false;
                IsAutoBackCalulate = false;
                ChkEKPME3.IsEnabled = false;
                btnPushQty.IsEnabled = false;
                zoneMainFabric.Visibility = Visibility.Hidden;
            }
            btnRefreshEKP.Visibility = Visibility.Visible;
            btnApprove.Visibility = Visibility.Visible;
            btnPushQty.Visibility = Visibility.Visible;
            ChkFixedQty.IsChecked = false;
            if (dwEKPOS["EKPBEKZ"].ToString() == "N")
            {
                ChkFixedQty.IsChecked = true;
                btnRefreshEKP.Visibility = Visibility.Visible;
            }
            else
            {
                ChkFixedQty.IsChecked = false;
                btnRefreshEKP.Visibility = Visibility.Visible;
                btnApprove.Visibility = Visibility.Visible;
                btnPushQty.Visibility = Visibility.Visible;
            }
            if (ApplicationBase.Instance.User.Trim() == "YCAV")
            {
                btnRefreshEKP.Visibility = Visibility.Visible;
                btnApprove.Visibility = Visibility.Visible;
                btnPushQty.Visibility = Visibility.Visible;
            }
            btnFilter_Click(null, null);
            try
            {

                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    WidthRatio = 0;
                    SResult = m_Data.GetDECK(SetData, dwEKPOS);
                    SetData.ICC_ABKOPF.Clear();
                    if (SResult.Tables.Contains("ICC_ABKOPF"))
                    {
                        SetData.Merge(SResult.Tables["ICC_ABKOPF"]);
                    }
                    SetDetail.Clear();
                    SetDetail.Merge(SResult);
                    ResetData(dwEKPOS, ConvertRatio);
                    btnQtyCost_Click(null, null);
                    zoneQty.Visibility = Visibility.Hidden;
                    zoneQty.IsEnabled = false;
                    zoneQty.IsExpanded = false;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnUpdateIntex_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "数据更新中"))
                {
                    ChangeStatus("98", "SP");
                    string text = m_Data.Save2Intex(SetDetail);
                    txtEKP.Text = text + txtEKP.Text;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                vwEKPOS.View.MoveCurrentTo(-1);
                vwEKPOS.View.MoveCurrentToPosition(lastindex);
            }
        }
        private void btnApprove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                btnApprove.Visibility = Visibility.Collapsed;
                ChangeStatus("89", "SP");
                string text = m_Data.Save2Intex(SetDetail);
                txtEKP.Text = text + txtEKP.Text;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                vwEKPOS.View.MoveCurrentTo(-1);
                vwEKPOS.View.MoveCurrentToPosition(lastindex);
            }
        }
        private bool PassCheck(DataSet src)
        {
            bool result = true;
            if (!IsAutoBackCalulate)
                return true;
            try
            {
                decimal qty = System.Convert.ToDecimal(txtEKNQTy.Text.ToString());
                var query = from DataRow row in SetDetail.ICC_WDECKMAT.AsEnumerable()
                            where row["ART"].ToString().Trim() == dwEKPOS["EKPART"].ToString().Trim()
                            && row["VAR"].ToString().Trim() == dwEKPOS["EKPVAR"].ToString().Trim()
                            && row["FARBE"].ToString().Trim() == dwEKPOS["EKPFARBE"].ToString().Trim()
                            select row;
                foreach (DataRow row in query)
                {
                    decimal RLFD = System.Convert.ToDecimal(row["FGRLFD"].ToString());
                    if (row["UM"].ToString() == "KG")
                    {
                        //qty = qty / RLFD * 1000;
                        qty = System.Math.Round(qty / RLFD * 1000, 0);
                    }
                }
                decimal rqty = 0;
                var qxd = from XD in src.Tables["ICC_ABAUFM"].AsEnumerable()
                          where XD["ABAAUFKRS"].ToString() == dwEKPOS["EKPAUFKRS"].ToString()
                           && XD["ABAAUFLFD"].ToString().Trim() == dwEKPOS["EKPAUFLFD"].ToString().Trim()
                           && XD["ABAAUFPOS"].ToString() == dwEKPOS["EKPAUFPOS"].ToString()
                          group XD by new
                          {
                              ART = XD["ABAAUFKRS"].ToString(),
                              VAR = XD["ABAAUFLFD"].ToString(),
                              FARBE = XD["ABAAUFPOS"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                                  ABAQTYRESERVE = g.Sum(XD => Convert.ToDecimal(XD.Field<Decimal>("ABAQTYRESERVE"))),
                              };

                foreach (var XD in qxd)
                {
                    rqty = System.Math.Round(XD.ABAQTYRESERVE, 0);
                }
                if (System.Convert.ToDouble(rqty - qty) >= 0.01)
                {
                    result = false;
                    if (MessageBox.Show("面料供应数量" + qty + "<需求数量" + rqty, "请确认!", MessageBoxButton.YesNoCancel) == MessageBoxResult.Yes)
                    {
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        void ResetData(DataRow dwInput, double _ConvertRatio)
        {
            DataRow dwEKP = dwInput;
            using (new LongOperation(this, "数据更新中"))
            {
                DataSet SResult = m_Data.ResetData(SetDetail, dwEKP);

                SetDetail.Clear();
                SetDetail.Merge(SResult);
                if (!PassCheck(SResult))
                {
                    SetDetail.Clear();
                    return;
                }
                #region======更新留位的原料数量==================
                var qMU = from DataRow dwMU in SetDetail.ICC_ABAUFM.AsEnumerable()
                          where dwMU["ABAAUFKRS"].ToString() == dwEKP["EKPAUFKRS"].ToString()
                          && dwMU["ABAAUFLFD"].ToString() == dwEKP["EKPAUFLFD"].ToString()
                          && dwMU["ABAAUFPOS"].ToString() == dwEKP["EKPAUFPOS"].ToString()
                          select dwMU;
                int countekp = qMU.Count();
                int icount = 0;
                double ABARESMG = 0;
                double USED = 0;
                foreach (DataRow dwMU in qMU)
                {
                    icount++;
                    ABARESMG = Convert.ToDouble(dwMU["ABARESMG"].ToString());
                    dwMU["ABAQTYRESERVE"] = System.Math.Round(ABARESMG * ConvertRatio, 1);
                }
                #endregion
                string mart = dwEKP["EKPART"].ToString();
                string mvar = dwEKP["EKPVAR"].ToString();
                string mcolor = dwEKP["EKPFARBE"].ToString();
                string UM = dwEKP["EKPMEEH"].ToString().Trim();

                foreach (DataRow prod in SetDetail.ICC_ABPOS_MAT.Rows)
                {
                    decimal HPCQTY = 0;//其他渠道总数量
                    decimal FGRLFD = 0;
                    decimal PCQTY = 0;
                    decimal PPQTY = 0;
                    decimal mqty = 0;
                    var qxd = from DataRow XD in SetDetail.ICC_ABAUFM.AsEnumerable()
                              where XD["ABAVKAUFKRS"].ToString() == prod["KRS"].ToString()
                              && XD["ABAVKAUFLFD"].ToString() == prod["LFD"].ToString()
                              && XD["ABPART"].ToString() == prod["ABPART"].ToString()
                              && XD["ABPFARBE"].ToString() == prod["ABPFARBE"].ToString()
                                 && XD["ABAART"].ToString().Trim() == mart.Trim()
                             && XD["ABAFARBE"].ToString().Trim() == mcolor.Trim()
                              select XD;
                    foreach (DataRow XD in qxd)
                    {
                        FGRLFD = Convert.ToDecimal(XD["WAFGRLFD"].ToString());
                        if (XD["ABAAUFKRS"].ToString().Trim() == dwEKPOS["EKPAUFKRS"].ToString().Trim()
                             && XD["ABAAUFLFD"].ToString().Trim() == dwEKPOS["EKPAUFLFD"].ToString().Trim()
                             && XD["ABAAUFPOS"].ToString().Trim() == dwEKPOS["EKPAUFPOS"].ToString().Trim())
                        {
                            PCQTY += System.Convert.ToDecimal(XD["ABARESMG"].ToString());
                            PPQTY += System.Convert.ToDecimal(XD["ABAQTYRESERVE"].ToString());
                        }
                        else
                        {
                            HPCQTY += System.Convert.ToDecimal(XD["ABARESMG"].ToString());
                        }
                        prod["RATIO"] = XD["RATIO"];//比例 
                        prod["PQCDAT"] = FGRLFD;
                    }

                    if (UM == "KG")
                    {
                        PCQTY = Math.Round(System.Convert.ToDecimal((FGRLFD / 1000) * PCQTY), 2);
                        PPQTY = Math.Round(System.Convert.ToDecimal((FGRLFD / 1000) * PPQTY), 2);
                        HPCQTY = Math.Round(System.Convert.ToDecimal((FGRLFD / 1000) * HPCQTY), 2);
                    }

                    prod["PCQTY"] = PCQTY;//面料用量                    
                    prod["HPCQTY"] = HPCQTY;//其他渠道用量                    
                    prod["PPQTY"] = PPQTY;//计划分配数量   
                }
            }
        }
        private void btnQueryEKPOS_Click(object sender, EventArgs e)
        {
            try
            {//FSMANDANT	FSART	FSVAR
                string client = "00", wakz = "F", art = "", var = "", color = "", size = "       ";
                if (mtype.Trim() == "")
                { MessageBox.Show("必需选择类型"); return; }
                switch (mtype)
                {
                    case "成衣":
                        IsAutoBackCalulate = false;
                        wakz = " ";
                        art = ART;
                        break;
                    case "主面料":
                        wakz = "F";
                        IsAutoBackCalulate = true;
                        art = ART;
                        break;
                    case "面辅料":
                        IsAutoBackCalulate = false;
                        wakz = "F";
                        art = m_Data.Left(ART + "            ", 14);
                        break;
                    case "高级辅料":
                        IsAutoBackCalulate = false;
                        wakz = " ";
                        art = ART;
                        break;
                }
                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    SResult = m_Data.GetEKPOS(client, wakz, art, var, color, size, EKPBEKZ, EKPME3);
                    SetData.Clear();
                    SetData.Merge(SResult);
                    if (vwEKPOS.View != null)
                    {
                        vwEKPOS.View.MoveCurrentToFirst();
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnQuery_Click(object sender, EventArgs e)
        {

            try
            {
                string client = "00", wakz = "F", art = "", var = "", color = "", size = "       ";
                if (mtype.Trim() == "")
                {
                    MessageBox.Show("必需选择类型");
                    return;
                }
                switch (mtype)
                {
                    case "成衣":
                        IsAutoBackCalulate = false;
                        wakz = " ";
                        break;
                    case "主面料":
                        wakz = "F";
                        IsAutoBackCalulate = true;
                        break;
                    case "面辅料":
                        IsAutoBackCalulate = false;
                        wakz = "F";
                        break;
                    case "高级辅料":
                        IsAutoBackCalulate = false;
                        wakz = " ";
                        break;
                }
                DataSet SResult = new DataSet();
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = "00";
                dwData["EKPAUFKRS"] = KRS;
                dwData["EKPAUFLFD"] = LFD;
                dwData["EKPBEKZ"] = EKPBEKZ;
                if (this.ChkAny.IsChecked == true)
                {
                    SResult = m_Data.GetEKPOS(dwData, "A");
                }
                else
                {
                    SResult = m_Data.GetEKPOS(dwData, EKPME3);
                }
                SetData.Clear();
                SetData.Merge(SResult);
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataSet setRpt = new DataSet();
            if (setRpt == null)
                return;
            m_DataX = new YCAFSPLANData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new YCAFSPLANReport(dicParamater);
            m_rpt.dsSource = SetDetail;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        private void ChangeStatus(string code, string status)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                string art = dwEKPOS["EKPART"].ToString();
                string var = dwEKPOS["EKPVAR"].ToString();
                string color = dwEKPOS["EKPFARBE"].ToString();
                if (code == "89" && dwEKPOS["EKPBEKZ"].ToString() == " ")
                {
                    MessageBox.Show("不是最终数量，不允许确认成衣件数的推算!");
                    return;
                }

                string abpart = "";
                string abpfarbe = "";
                var qwd = from WD in SetDetail.WDECK.AsEnumerable()
                          where WD["WDART"].ToString().Trim() == art.Trim()
                           && WD["WDVAR"].ToString().Trim() == var.Trim()
                           && WD["WDFARBE"].ToString().Trim() == color.Trim()
                          group WD by new
                           {
                               KRS = WD["WDAUFKRS"].ToString(),
                               LFD = WD["WDAUFLFD"].ToString(),
                               POS = WD["WDAUFPOS"].ToString(),
                           }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                string processedab = "";

                foreach (var WD in qwd)
                {
                    DataRow dwABPOS = null;
                    string krs = WD.KEY.KRS;
                    string lfd = WD.KEY.LFD;
                    string pos = WD.KEY.POS;
                    var qABP = from DataRow rx in SetDetail.ABPOS.AsEnumerable()
                               where rx["ABPAUFKRS"].ToString() == krs
                               && rx["ABPAUFLFD"].ToString() == lfd
                               && rx["ABPAUFPOS"].ToString() == pos
                               select rx;
                    foreach (DataRow rx in qABP)
                    {
                        dwABPOS = rx;
                        abpart = dwABPOS["ABPART"].ToString();
                        abpfarbe = dwABPOS["ABPFARBE"].ToString();
                        break;
                    }
                    if (!processedab.Contains(krs + lfd + abpart + abpfarbe + ";"))
                    {
                        processedab += krs + lfd + abpart + abpfarbe + ";";
                        var qab = from AB in SetDetail.ICC_ABPOS_MAT.AsEnumerable()
                                  where AB["KRS"].ToString() == krs
                                  && AB["LFD"].ToString() == lfd
                                      && AB["ABPART"].ToString() == abpart
                                   && AB["ABPFARBE"].ToString() == abpfarbe
                                  && AB["ABPSPGRD"].ToString() == "89"
                                  select AB;
                        if (qab.Count() > 0)
                        {
                            MessageBox.Show("该面料数量已经确认，辅料可能已经采购");
                            if (!m_Data.UserID.Contains("YCAV"))
                                return;
                        }

                    }

                    foreach (DataRow rx in qABP)
                    {
                        dwABPOS = rx;
                        dwABPOS["ABPSPGRD"] = code;
                        dwABPOS["ABPGESSTA"] = status;
                        dwABPOS["ABPAENDBEN"] = m_Data.UserID;
                        dwABPOS["ABPAENDDAT"] = m_Data.date;
                        m_Data.INTEXDBC.UpdateABPOS(dwABPOS, 5);
                        m_Data.INTEXBL.CheckEvents("A001020", dwABPOS, "主面料审核完毕", null);
                        m_Data.INTEXBL.CheckEvents("A001021", dwABPOS, "主面料审核完毕", null);
                        m_Data.INTEXBL.CheckEvents("A001022", dwABPOS, "主面料审核完毕", null);
                    }
                    var qmat = from DataRow rx in SetDetail.ICC_ABPOS_MAT.AsEnumerable()
                               where rx["KRS"].ToString() == krs
                               && rx["LFD"].ToString() == lfd
                               && rx["ABPART"].ToString() == abpart
                               && rx["ABPFARBE"].ToString() == abpfarbe
                               select rx;
                    foreach (DataRow rx in qmat)
                    {
                        rx["ABPSPGRD"] = code;
                        m_Data.INTEXDBC.UpdateICC_ABPOS_MAT(rx, 1);
                    }
                    string processed = txtEKP.Text;
                    string text = dwABPOS["ABPAUFKRS"].ToString().Trim() + "-" + dwABPOS["ABPAUFLFD"].ToString().Trim() + abpart.Trim() + "-" + abpfarbe.Trim() + "已审核";
                    if (processed.Contains(text))
                        continue;
                    txtEKP.Text = text + Environment.NewLine + txtEKP.Text;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void AutoAllocation(DataRow dwEKP)
        {
            try
            {
                double ekqty = Convert.ToDouble(dwEKP["EKPME1"].ToString());
                string UM = dwEKP["EKPMEEH"].ToString();
                double ratio = 1;
                double ABAQTYRESERVE = 1;
                var qxd = from EKP in SetData.ICC_ABAUFM.AsEnumerable()
                          where EKP["ABAAUFTYP"].ToString() == "P"
                                               && EKP["ABAAUFKRS"].ToString() == dwEKP["EKPAUFKRS"].ToString()
                                               && EKP["ABAAUFLFD"].ToString() == dwEKP["EKPAUFLFD"].ToString()
                                               && EKP["ABAAUFPOS"].ToString() == dwEKP["EKPAUFPOS"].ToString()
                          group EKP by new
                          {
                              TYP = EKP["ABAAUFTYP"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                                  ABAQTYRESERVE = g.Sum(EKP => Convert.ToDouble(EKP.Field<Decimal>("ABAQTYRESERVE"))),
                              };
                foreach (var q in qxd)
                {
                    if (q.KEY.TYP.ToString() == "P")
                    {
                        ABAQTYRESERVE = q.ABAQTYRESERVE;
                    }
                }
                var query = from DataRow row in SetData.ICC_ABAUFM.AsEnumerable()
                            where row["ABAAUFTYP"].ToString() == "P"
                            && row["ABAAUFKRS"].ToString() == dwEKPOS["EKPAUFKRS"].ToString()
                            && row["ABAAUFLFD"].ToString() == dwEKPOS["EKPAUFLFD"].ToString()
                            && row["ABAAUFPOS"].ToString() == dwEKPOS["EKPAUFPOS"].ToString()
                            select row;
                foreach (DataRow row in query)
                {
                    double qty = System.Convert.ToDouble(row["ABAQTYRESERVE"].ToString());
                    double WAFGRLFD = 1;

                    if (UM.Trim() == "KG")
                    {
                        WAFGRLFD = System.Convert.ToDouble(row["WAFGRLFD"].ToString()) * 0.001;
                    }
                    ratio = qty / ABAQTYRESERVE;
                    row["ABAQTYRESERVE"] = System.Math.Round(ratio * ekqty / WAFGRLFD, 2);
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPushQty_Click(object sender, RoutedEventArgs e)
        {
            btnPushQty.Visibility = Visibility.Hidden;
            btnApprove.Visibility = Visibility.Visible;
            if (!IsAutoBackCalulate)
            {
                AutoAllocation(dwEKPOS);
                return;
            }
            if (dwEKPOS == null)
                return;
            if (txtEKQTy.Text.Trim() == "")
                txtEKQTy.Text = "0";
            string art = dwEKPOS["EKPART"].ToString();
            string var = dwEKPOS["EKPVAR"].ToString();
            string color = dwEKPOS["EKPFARBE"].ToString();
            string UM = dwEKPOS["EKPMEEH"].ToString();
            string EKPAUFKRS = dwEKPOS["EKPAUFKRS"].ToString();
            string EKPAUFLFD = dwEKPOS["EKPAUFLFD"].ToString();
            string EKPAUFPOS = dwEKPOS["EKPAUFPOS"].ToString();

            //decimal ekqty = System.Convert.ToDecimal(txtEKNQTy.Text.ToString());
            double ekqty = System.Convert.ToDouble(dwEKPOS["EKPME1"].ToString());

            //获取

            int count = SetDetail.ICC_ABPOS_MAT.Rows.Count;

            double qtyused = 0;
            int ix = 0;
            /*
             * 1.ICC_ABAUFM:S行，仓库留位数量（需求数量=0）;M行采购数量[总需求数量]，L行，补采购数量（需求数量=0）
             * 2.获取原料数量，如果存在由其他单提供的数量，则需要考虑：PCQTY总数量，增加一个其他渠道数量，增加一个本渠道数量
             */
            foreach (DataRow row in SetDetail.ICC_ABPOS_MAT.Rows)
            {
                ix++;
                #region=========收集其他渠道数量===========
                double HPCQTY = Convert.ToDouble(row["HPCQTY"].ToString());//其他渠道总数量
                double ratio = 0;//BOM比例    
                double FGRLFD = 0;
                ratio = Convert.ToDouble(row["RATIO"].ToString());
                FGRLFD = Convert.ToDouble(row["PQCDAT"].ToString());
                if (UM.Trim() == "KG")
                {
                    ratio = ratio * FGRLFD / 1000;
                }
                #endregion

                #region=========本渠道数量===========
                double PPQTY = System.Convert.ToDouble(row["PPQTY"].ToString());//本渠道总数量

                if (ix == count)
                {
                    PPQTY = ekqty - qtyused;
                    if (PPQTY < 0)
                    {
                        MessageBox.Show("请重新分配数量");
                        return;
                    }
                    row["PPQTY"] = PPQTY;
                }
                qtyused += PPQTY;
                #endregion

                #region=========推算成衣件数==============
                double PCQTY = Math.Round(PPQTY / ConvertRatio, 1);//有效的总数量 
                row["PCQTY"] = PCQTY;//总数量  
                double TPCS = Math.Floor((PCQTY + HPCQTY)/ ratio);
                double TQTYP = Convert.ToDouble(row["TQTYP"].ToString());
                double SP = Convert.ToDouble(row["SP"].ToString());
                double MP = Convert.ToDouble(row["MP"].ToString());
                double LP = Convert.ToDouble(row["LP"].ToString());
                double XLP = Convert.ToDouble(row["XLP"].ToString());

                double S = Math.Round(TPCS * SP / TQTYP);
                double M = Math.Round(TPCS * MP / TQTYP);
                double L = Math.Round(TPCS * LP / TQTYP);
                double XL = Math.Round(TPCS * XLP / TQTYP);
                if (XLP > 0)
                {
                    XL = TPCS - (L + M + S);
                }
                else
                {
                    if (LP > 0)
                    {
                        L = TPCS - (M + S);
                    }
                    else
                    {
                        if (MP > 0)
                        {
                            M = TPCS - (S);
                        }
                        else
                        {
                            S = TPCS;
                        }
                    }
                }
                row["S"] = S;
                row["M"] = M;
                row["L"] = L;
                row["XL"] = XL;

                var query = from DataRow rx in SetDetail.ABPOS.AsEnumerable()
                            where rx["ABPART"].ToString().Trim() == row["ABPART"].ToString().Trim()
                            && rx["ABPFARBE"].ToString().Trim() == row["ABPFARBE"].ToString().Trim()
                            && rx["ABPAUFKRS"].ToString().Trim() == row["KRS"].ToString().Trim()
                            && rx["ABPAUFLFD"].ToString().Trim() == row["LFD"].ToString().Trim()
                            select rx;
                foreach (DataRow rx in query)
                {
                    switch (rx["ABPGROE"].ToString().Trim())
                    {
                        case "S":
                            rx["ABPSOLLMENGE7"] = Convert.ToDouble(row["S"].ToString()) * 100;
                            break;
                        case "M":
                            rx["ABPSOLLMENGE7"] = Convert.ToDouble(row["M"].ToString()) * 100;
                            break;
                        case "L":
                            rx["ABPSOLLMENGE7"] = Convert.ToDouble(row["L"].ToString()) * 100;
                            break;
                        case "XL":
                            rx["ABPSOLLMENGE7"] = Convert.ToDouble(row["XL"].ToString()) * 100;
                            break;
                    }
                }
                #endregion
            }
            SetDetail.AcceptChanges();
            ResetData(dwEKPOS, ConvertRatio); 
        }

        private void btnRefreshEKP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsAutoBackCalulate)
                {
                    AutoAllocation(dwEKPOS);
                }
                DataRow dwData = dwEKPOS.Table.Clone().NewRow();
                dwData.ItemArray = dwEKPOS.ItemArray;
                double qty = System.Convert.ToDouble(dwData["EKPME1"].ToString()) * 100;
                double wert = 0;
                for (int j = 1; j < 6; j++)
                {
                    wert += System.Convert.ToDouble(dwEKPOS["EKPZUABWE" + j].ToString()) / 1000;
                }
                dwData["EKPME1"] = qty;
                double price = System.Convert.ToDouble(dwData["EKPPRME"].ToString()) * 0.001;
                if (dwData["EKPPRME"].ToString().Trim() == "")
                {
                    dwData["EKPPRME"] = "0";
                }
                dwData["EKPWE1"] = System.Math.Round(qty * price + wert * 100, 0);
                if (ChkFixedQty.IsChecked == true)
                {
                    dwData["EKPBEKZ"] = "N";
                    dwData["EKPBEDAT"] = m_Data.date;

                }
                else
                {
                    dwData["EKPBEKZ"] = " ";
                    dwData["EKPBEDAT"] = "0       ";
                }
                if (dwData["EKPVTERMIN"].ToString().Trim() == "")
                {
                    dwData["EKPVTERMIN"] = dwData["EKPLTERM"];
                }
                dwEKPOS["EKPBEKZ"] = dwData["EKPBEKZ"];
                dwEKPOS["EKPBEDAT"] = dwData["EKPBEDAT"];
                dwData["EKPAENB"] = m_Data.UserID;
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 2);
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 21);
                m_Data.INTEXDBC.UpdateEKKOPF(dwData, 21);
                dwData = SetData.EKKOPF.NewRow();
                dwData["EKKMANDANT"] = "00";
                dwData["EKKAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKKAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                m_Data.INTEXDBC.UpdateEKKOPF(dwData, 2);
                string text = dwData["EKKAUFKRS"].ToString() + dwData["EKKAUFLFD"].ToString() + "已保存";
                txtEKP.Text = text + Environment.NewLine + txtEKP.Text;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                btnWFConfirm_Click(null, null);
            }
        }
        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            string filter = "ABAAUFTYP ='P' AND ABAAUFKRS='" + dwEKPOS["EKPAUFKRS"].ToString() + "' AND ABAAUFLFD='"
                + dwEKPOS["EKPAUFLFD"].ToString() + "' AND ABAAUFPOS='" + dwEKPOS["EKPAUFPOS"].ToString() + "'";
            cvABAUFM.CustomFilter = filter;
        }

        private void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            cvABAUFM.CustomFilter = null;
        }
        private void cbSECTION_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            txtSection.Text += cb.SelectedValue + ";";
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (dwEKPOS == null)
            {
                MessageBox.Show("请选择采购单!");
                return;
            }
            if (dwABPOS_MAT == null)
            {
                MessageBox.Show("请选择需要移除的款!");
                return;
            }
            if (dwABPOS_MAT["ABPSPGRD"].ToString().Trim() == "89"
                || dwABPOS_MAT["ABPSPGRD"].ToString().Trim() == ""
                )
            {
                if (MessageBox.Show("已经确认，禁止删除!如果确认，请继续！", "请确认", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                {
                    return;
                }
            }
            string KRS = dwABPOS_MAT["KRS"].ToString();
            string LFD = dwABPOS_MAT["LFD"].ToString();
            string ABPART = dwABPOS_MAT["ABPART"].ToString();
            string ABPFARBE = dwABPOS_MAT["ABPFARBE"].ToString();
            string abaidlist = "";
            var query = from DataRow row in SetData.ICC_ABAUFM.AsEnumerable()
                        where row["ABAAUFTYP"].ToString() == "P"
                        && row["ABAAUFKRS"].ToString() == dwEKPOS["EKPAUFKRS"].ToString()
                        && row["ABAAUFLFD"].ToString() == dwEKPOS["EKPAUFLFD"].ToString()
                        && row["ABAAUFPOS"].ToString() == dwEKPOS["EKPAUFPOS"].ToString()
                        && row["ABAVKAUFKRS"].ToString() == KRS
                        && row["ABAVKAUFLFD"].ToString() == LFD
                        && row["ABPART"].ToString() == ABPART
                        && row["ABPFARBE"].ToString() == ABPFARBE
                        select row;
            if (query.Count() == 1)
            {
                //MessageBox.Show("最后一行了，禁止删除!");
                return;
            }
            foreach (DataRow row in query)
            {

                abaidlist += "'" + row["ABAID"].ToString() + "',";
                m_Data.INTEXDBC.DeleteICC_ABAUFM("", "", "", "", row, 4);
                if (row["FSSZUSCHN"].ToString().Trim() == "J")
                {
                    DataRow dwData = SetData.ABPOS.NewRow();
                    dwData["ABPMANDANT"] = "00";
                    dwData["ABPAUFKRS"] = row["ABAVKAUFKRS"];
                    dwData["ABPAUFLFD"] = row["ABAVKAUFLFD"];
                    dwData["ABPAUFPOS"] = row["ABAVKAUFPOS"];
                    dwData["ABPSPGRD"] = "99";
                    dwData["ABPGESSTA"] = "SP";
                    dwABPOS["ABPAENDBEN"] = m_Data.UserID;
                    dwABPOS["ABPAENDDAT"] = m_Data.date;
                    m_Data.INTEXDBC.UpdateABPOS(dwData, 1);
                }
            }
            DataRow[] rows = SetDetail.ICC_ABPOS_MAT.Select("KRS = '" + KRS + "' AND LFD='" + LFD + "' AND ABPART='" + ABPART + "' AND ABPFARBE='" + ABPFARBE + "'");
            foreach (DataRow rw in rows)
            {
                rw.Delete();
            }
            rows = SetData.ICC_ABAUFM.Select("ABAID IN (" + abaidlist + ")");
            foreach (DataRow rw in rows)
            {
                rw.Delete();
            }
            SetData.AcceptChanges();
        }
        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                SetEnv.WDECK.Clear();
                SetEnv.FDECK.Clear();
                DataSet SResult = m_Data.LoadGXDECKData(mtype, txtSection.Text, EKPBEKZ);
                SetEnv.Merge(SResult);
            }
        }
        void CheckFunction(string mtyp)
        {
            zoneMainFabric.Visibility = Visibility.Hidden;
            btnPushQty.IsEnabled = false;
            switch (mtyp)
            {
                case "成衣":
                    IsAutoBackCalulate = false;
                    btnUpdateIntex.IsEnabled = false;
                    btnApprove.IsEnabled = false;
                    ChkEKPME3.IsEnabled = true;
                    break;
                case "主面料":
                    zoneMainFabric.Visibility = Visibility.Visible;
                    btnUpdateIntex.IsEnabled = true;
                    btnPushQty.IsEnabled = true;
                    btnApprove.IsEnabled = true;
                    IsAutoBackCalulate = true;
                    ChkEKPME3.IsEnabled = true;
                    break;
                case "面辅料":
                    btnUpdateIntex.IsEnabled = false;
                    btnApprove.IsEnabled = false;
                    IsAutoBackCalulate = false;
                    ChkEKPME3.IsEnabled = true;
                    break;
                case "高级辅料":
                    btnUpdateIntex.IsEnabled = false;
                    btnApprove.IsEnabled = false;
                    IsAutoBackCalulate = false;
                    ChkEKPME3.IsEnabled = true;
                    ChkEKPME3.IsChecked = true;
                    break;
            }
        }
        private void cbWTYE_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;
            SetEnv.WDECK.Clear();
            string mtyp = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
            CheckFunction(mtyp);
        }
        private void btnQtyCost_Click(object sender, RoutedEventArgs e)
        {
            if (dwEKPOS == null)
                return;
            try
            {
                /*zoneQty.Visibility = Visibility.Visible;
                zoneQty.IsExpanded = true;
                zoneQty.IsEnabled = true;*/
                txtEKNQTy.Text = txtEKQTy.Text;
                if (dwEKPOS["EKPEFARBE"].ToString().Trim() == "")
                    return;
                if (WidthRatio == 0)
                    WidthRatio = 1;
                double EKPME1 = Convert.ToDouble(dwEKPOS["EKPME1"].ToString());

                string rqty = dwEKPOS["EKPEFARBE"].ToString();
                if (rqty.Trim() == "")
                    rqty = EKPME1.ToString(); ;

                EKNQTY = System.Convert.ToDouble(rqty);
                ConvertRatio = EKPME1 / EKNQTY;
                if (dwEKPOS["EKPEART"].ToString().Trim() == "")
                    return;
                string[] cost = dwEKPOS["EKPEART"].ToString().Split(new Char[] { ';' });
                wgPackage = System.Convert.ToDouble(cost[0]);
                WidthRatio = System.Convert.ToDouble(cost[1]);
                txtWidthRatio.Text = WidthRatio.ToString();
                txtPackgewg.Text = wgPackage.ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            //txtMax.Text = wastageek.ToString();           
            //wgPackage
            //EKPEART
            //EKPEFARBE
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (dwEKPOS == null)
                return;
            zoneQty.Visibility = Visibility.Hidden;
            zoneQty.IsExpanded = false;
            double wastageek = 0;
            double EKPME1 = System.Convert.ToDouble(dwEKPOS["EKPME1"].ToString());
            if (WidthRatio == 0)
            {
                WidthRatio = 1;
                //WidthRatio = System.Math.Round(NWidth /Width, 3);
                //wastageek =Math.Round(1 / (1 + (double)WidthRatio * 0.01),2);
            }
            //WidthRatio =Math.Round(Convert.ToDecimal(1 / (1 + Convert.ToDouble(txtMax.Text) * 0.01)),2);
            EKNQTY = Math.Round((EKPME1 - wgPackage) * (WidthRatio), 2);
            //dwEKPOS["EKPEART"] = wgPackage + ";" + WidthRatio + ";";
            dwEKPOS["EKPEFARBE"] = EKNQTY;
            //wgPackage
            //EKPEART
            //EKPEFARBE
        }
        private void txtEKQTy_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {

            if (e.Key == System.Windows.Input.Key.Enter || e.Key == System.Windows.Input.Key.Return)
            {
                double value = Convert.ToDouble(txtEKQTy.Text);
                double max = Convert.ToDouble(txtMax.Text);
                double min = Convert.ToDouble(txtMin.Text);
                if (value > (Convert.ToDouble(OldValue) * (1 + max * 0.01)))
                {
                    MessageBox.Show("已经超出上限!");
                    dwEKPOS["EKPME1"] = OldValue;
                    return;
                }
                /*
                if (value < (Convert.ToDouble(OldValue) * (1 - max * 0.01)))
                {
                    MessageBox.Show("已经低于下限!");
                    dwEKPOS["EKPME1"] = OldValue;
                    return;
                }*/
                OldValue = value;
                dwEKPOS["EKPME1"] = value;
                btnConfirm_Click(null, null);
            }
        }
        private void btnWFConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (dwWFEvent == null)
                return;
            dwWFEvent["CONTROL"] = "OK";
            dwWFEvent["CHGUSER"] = m_Data.UserID;
            dwWFEvent["CHGTIME"] = Helpers.Helper.Left(System.DateTime.Now.ToString(), 20);
            m_Data.INTEXDBC.UpdateICC_WFEVENTS(dwWFEvent);
        }

    }
}
